package com.learn.security.core.social.weixin.config;

import com.learn.security.core.properties.SecurityProperties;
import com.learn.security.core.properties.WeixinProperties;
import com.learn.security.core.social.MyConnectView;
import com.learn.security.core.social.SocialConfig;
import com.learn.security.core.social.weixin.connet.WeixinConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.web.servlet.View;

/**
 * 微信登录配置
 *
 * @author zhailiang
 *
 */
@Configuration
@ConditionalOnProperty(prefix = "learn.security.social.weixin", name = "app-id")
public class WeixinAutoConfiguration extends SocialConfig {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {
        WeixinProperties weixinConfig = securityProperties.getSocial().getWeixin();
        return new WeixinConnectionFactory(weixinConfig.getProviderId(), weixinConfig.getAppId(),
                weixinConfig.getAppSecret());
    }

    /**
     * 微信绑定/解绑操作的状态返回
     * 解绑跳的视图：connect/weixinConnect
     * 绑定跳的视图：connect/weixinConnected
     *
     * {@link com.learn.security.core.social.MyConnectView}
     */
    @Bean({"connect/weixinConnect", "connect/weixinConnected"})
    @ConditionalOnMissingBean(name = "weixinConnectedView")
    public View weixinConnectedView() {
        return new MyConnectView();
    }


}
